package com.scuola.scuoladocente;

import java.util.LinkedHashMap;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.Toast;

public class InserisciVoto extends Activity {

	private boolean nuovoVoto = true;
	private String voto = null, data = null, tipologia = null, votoId = null;
	private Spinner spinnerVoto;
	private Spinner spinnerTipologia;
	private DatePicker dp;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inserisci_voto);
        spinnerVoto = (Spinner) findViewById(R.id.spinner_voti);
        spinnerTipologia = (Spinner) findViewById(R.id.spinnerTipologia);
        dp = (DatePicker)findViewById(R.id.iv_datePicker);
        
        if(getIntent().getStringExtra("voto") != null){
	        votoId = getIntent().getStringExtra("votoId");
        	ArrayAdapter<CharSequence> adapterVoto = (ArrayAdapter<CharSequence>) spinnerVoto.getAdapter();
		    int position = adapterVoto.getPosition(getIntent().getStringExtra("voto"));
		    spinnerVoto.setSelection(position);
		    
		    String data[] = getIntent().getStringExtra("data").split("\\-");
		    int anno = Integer.parseInt(data[0]);
		    int mese = Integer.parseInt(data[1])-1;
		    int giorno = Integer.parseInt(data[2]);
		    dp.updateDate(anno, mese, giorno);
		    
		    ArrayAdapter<CharSequence> adapterTipologia = (ArrayAdapter<CharSequence>) spinnerTipologia.getAdapter();
		    int positionTipologia = adapterTipologia.getPosition(getIntent().getStringExtra("tipologia"));
		    spinnerTipologia.setSelection(positionTipologia);
		    nuovoVoto = false;
		    
        }else{
        	votoId = "";
        }
        
        final Button btnEntra = (Button) findViewById(R.id.btn_iv_salvaModifiche);        
        btnEntra.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	try {
            		voto = spinnerVoto.getSelectedItem().toString();
            		tipologia = spinnerTipologia.getSelectedItem().toString();
            		data = dp.getYear()+"-"+(dp.getMonth()+1)+"-"+dp.getDayOfMonth();
            		AsyncTask<Void, Void, String> setVotoResponse = new setVoto().execute();
        			String msg = setVotoResponse.get();
        			if(msg.equals("OK")){
        				Toast msgToast = Toast.makeText(getApplicationContext(), "Voto salvato correttamente", Toast.LENGTH_SHORT);
        				msgToast.show();
        				finish();
        			}else{
        				Toast msgToast = Toast.makeText(getApplicationContext(), "Errore invio! Riprova!", Toast.LENGTH_SHORT);
        				msgToast.show();
        			}
        			
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            }
        });
        
    }
    
        
    private class setVoto extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... arg0) {
			try{	
				LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
				hm.put("classId", getIntent().getStringExtra("classeId"));
				hm.put("materiaId", getIntent().getStringExtra("materiaId"));
				hm.put("studenteId", getIntent().getStringExtra("studenteId"));
				hm.put("docenteId", getIntent().getStringExtra("docenteId"));
				hm.put("nuovoVoto", String.valueOf(nuovoVoto));
				hm.put("voto", voto);
				hm.put("data", data);
				hm.put("tipologia", tipologia);
				hm.put("votoId", votoId);
				String strResponse = operations.wsCall("setVoto", hm).toString();
				return strResponse;
				
			} catch (Exception e) {
				System.err.println("setAvviso errore (Exception)");
				e.printStackTrace();
				return null;
			}
		
		}
    }
    
        
}
